package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.dto.DepartmentDTO;
import com.wjj.sixinone.common.pojo.entity.Department;
import com.wjj.sixinone.common.pojo.excel.DepartmentExcel;
import com.wjj.sixinone.common.pojo.vo.DepartmentForm;
import com.wjj.sixinone.common.pojo.vo.DepartmentVO;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.handler.ExcelExportHandle;
import com.wjj.sixinone.mapper.DepartmentMapper;
import com.wjj.sixinone.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小王不秃头
 * @since 2024-07-22
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ExcelExportHandle excelExportHandle;

    @Override
    public Result selectDepartmentHeadByLimit(DepartmentVO departmentVO) {
        Integer page = departmentVO.getPage();
        Integer limit = departmentVO.getLimit();
        Integer index = (page - 1) * limit;
        DepartmentDTO departmentDTO = new DepartmentDTO();
        BeanUtils.copyProperties(departmentVO, departmentDTO);
        departmentDTO.setIndex(index);
        List<Department> departmentList = departmentMapper.selectDepartmentByLimit(departmentDTO);

        Integer count = departmentMapper.count(departmentDTO);


        return Result.ok().put("data", departmentList).put("count", count);
    }

    @Override
    public void uploadExcel(HttpServletResponse httpServletResponse) {

        List<Department> departments = departmentMapper.selectList(new LambdaQueryWrapper<Department>().select(Department::getDepartmentName, Department::getDepartmentHead
                , Department::getUpdateUser, Department::getCreateUser, Department::getCreateTime, Department::getUpdateTime
        ));
        List<DepartmentExcel> departmentExcels = new ArrayList<>();
        for (Department department : departments) {
            DepartmentExcel departmentExcel = new DepartmentExcel();
            BeanUtils.copyProperties(department, departmentExcel);
            departmentExcels.add(departmentExcel);

        }
        excelExportHandle.export(httpServletResponse, "部门信息", departmentExcels, DepartmentExcel.class);

    }

    @Override
    public Result updateDepartmentById(DepartmentForm department) {
        String departmentName = department.getDepartmentName();
        Integer departmentId = department.getId(); // Assume you have a method to get the ID

        if (departmentName == null || departmentName.trim().isEmpty()) {
            throw new GlobalException("部门名称不能为空", 400);
        }
        checkDepartment(departmentName, departmentId); // Pass the current ID to exclude it from check

        Department departmentToUpdate = new Department();
        BeanUtils.copyProperties(department, departmentToUpdate);
        int result = departmentMapper.updateById(departmentToUpdate);
        if (result < 1) {
            throw new GlobalException(ExceptionEnum.UPDATE_ERROR);
        }

        return Result.ok();
    }


    @Override
    public Result addDepartment(DepartmentForm department) {
        if (department.getDepartmentName() == null || department.getDepartmentName().trim().isEmpty()) {
            throw new GlobalException("部门名称不能为空", 400);
        }
        checkDepartment(department.getDepartmentName(), null); // No ID to exclude for add operation

        Department departmentToInsert = new Department();
        BeanUtils.copyProperties(department, departmentToInsert);
        int result = departmentMapper.insert(departmentToInsert);
        if (result < 1) {
            throw new GlobalException(ExceptionEnum.INSERT_ERROR);
        }

        return Result.ok();
    }


    @Override
    public Result deleteById(String id) {
        int i = departmentMapper.deleteById(id);
        if (i < 1) {
            throw new GlobalException(ExceptionEnum.DELETE_ERROR);
        }
        return Result.ok();
    }

    private boolean checkDepartment(String departmentName, Integer currentId) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Department::getId)
                .eq(Department::getDepartmentName, departmentName);

        // Exclude the current department if an ID is provided
        if (currentId != null) {
            queryWrapper.ne(Department::getId, currentId);
        }

        Department departmentDb = departmentMapper.selectOne(queryWrapper);
        if (departmentDb != null) {
            throw new GlobalException("部门名称已经存在", 400);
        }
        return true;
    }



}


